//
//  workerManager.cpp
//  C++Learn
//
//  Created by ryu on 2021/8/21.
//

#include "workerManager.hpp"
using namespace std;
#include <iostream>

void WorkerManager::showMenu(){
    cout<<"==========================="<<endl;
    cout<<"|*******职工管理系统******|"<<endl;
    cout<<"|******0.退出系统********｜"<<endl;
    cout<<"|******1.添加员工信息*****｜"<<endl;
    cout<<"|******2.显示员工信息*****｜"<<endl;
    cout<<"|******3.删除离职职工*****｜"<<endl;
    cout<<"|******4.修改职工信息*****｜"<<endl;
    cout<<"|******5.查找职工信息*****｜"<<endl;
    cout<<"|******6.按照编号排序*****｜"<<endl;
    cout<<"|******7.清空所有文档*****｜"<<endl;
    cout<<"==========================="<<endl;
}

//

WorkerManager::WorkerManager(){
    
    //初始化人数
    this->num = 0;
    
    //初始化数组
    this->empArry = new Worker*[5];
    
    //初始化数组大小
    this->size = 5;
    
    init();
    
    
}

WorkerManager::~WorkerManager(){
    //new 创建的对象是在堆里，需要手动释放
    delete[] this->empArry;
    //防止野指针。(指针在清空后，会随机指向堆内的对象)
    this->empArry = NULL;
    this->size = 0;
    this->num = 0;
    cout<<"已清空"<<endl;
}

void WorkerManager::exitSys(){
    cout<<"已退出"<<endl;
    exit(0);
}

bool WorkerManager::addWorker(){
    cout<<"您要添加的人数"<<endl;
    int n = 0;
    cin>>n;
    int sum = n + this->num;
    if(n <= 0)
        return false;
    else{
        //计算数组空间是否需要初始化
        if(this->size < sum){
            //开辟新空间
            Worker **newSpace = new Worker*[size * 2];
            for(int i = 0;i < this->num;i++){
                newSpace[i] = this->empArry[i];
            }
            //释放原先空间
            delete [] this->empArry;
            this->empArry = newSpace;
        }
        for(int i = 0; i < n;i++){
            cout<<"您要添加的职工类别"<<endl;
            cout<<"1. 普通员工"<<endl;
            cout<<"2. 经理"<<endl;
            cout<<"3. Boss"<<endl;
            int in = 0;
            cin>>in;
            int id = 0;
            int dp_id = 0;
            string name = "";
            cout<<"请输入员工id"<<endl;
            cin>>id;
            cout<<"请输入员工部门id"<<endl;
            cin>>dp_id;
            cout<<"请输入员工名字"<<endl;
            cin>>name;
            Worker *worker = NULL;
            switch (in) {
                case 1:
                    worker = new Employee(id,dp_id,name);
                    break;
                case 2:
                    worker = new Manager(id,dp_id,name);
                    break;
                case 3:
                    worker = new Boss(id,dp_id,name);
                    break;
                default:
                    break;
            }
            this->empArry[this->num++] = worker;
        }
        cout<<"添加完成"<<endl;
        return true;
    }
}

void WorkerManager::showList(){
    int n = this->num;
    cout<<"==========================="<<endl;
    cout<<"当前人数： "<<n<<endl;
    for(int i = 0; i < n;i++){
        this->empArry[i]->showInfo();
    }
    cout<<"==========================="<<endl;
}

void WorkerManager::del(){
    if(this->num == 0){
        cout<<"当前员工数为0"<<endl;
        return;
    }
    cout<<"请输入要删除员工的ID"<<endl;
    int del = 0;
    cin>>del;
    int id = -1;
    //根据ID查找(录入可能是无序的所以直接迭代查找，而且方便删除后重组)
    id = check(del);
    cout<<id<<endl;
    if(id == -1){
        cout<<"抱歉，未找到!";
        return;
    }
    for(int i = id;id < this->num - 1;i++){
        this->empArry[i] = this->empArry[i+1];
    }
    this->empArry[this->num-1] = NULL;
    this->num--;
    cout<<"已删除"<<endl;
}

void WorkerManager::update(){
    cout<<"输入要更新员工的id"<<endl;
    int id = -1;
    cin>>id;
    id = check(id);
    if(id == -1){
        cout<<"查找的员工不存在"<<endl;
        return;
    }
    int newId;
    int newDpId;
    string newName;
    cout<<"输入修改后的id"<<endl;
    cin>>newId;
    cout<<"输入修改后的部门ID"<<endl;
    cin>>newDpId;
    cout<<"输入修改后的名字"<<endl;
    cin>>newName;
    this->empArry[id]->id = newId;
    this->empArry[id]->dp_id = newDpId;
    this->empArry[id]->name = newName;
    cout<<"修改完成"<<endl;
}

int WorkerManager::check(int id){
    int n = this->num;
    for(int i = 0; i < n;i++){
        if(this->empArry[i]->id == id)
            return i;
    }
    return -1;
}

void WorkerManager::info(){
    cout<<"输入要查看员工的Id"<<endl;
    int id = -1;
    cin>>id;
    id = check(id);
    if(id == -1){
        cout<<"输入的员工不存在"<<endl;
        return;
    }
    this->empArry[id]->showInfo();
}

void WorkerManager::init(){
    cout<<"初始化"<<endl;
    for(int i = 3; i < 5;i++){
        Worker *worker = NULL;
        worker = new Employee(i,i,"emp"+std::to_string(i));
        this->empArry[this->num++] = worker;
        
    }
    
}
void WorkerManager::sort(){
    cout<<"按照员工编号排序"<<endl;
    for(int i = 0;i < this->num - 1;i++){
        for(int j = 0;j < this->num -i-1;j++){
            if(this->empArry[j] > this->empArry[j+1]){
                Worker *worker = this->empArry[j];
                this->empArry[j] = this->empArry[j+1];
                this->empArry[j+1] = worker;
            }
        }
    }
    int k = 0;
    cout<<"升序：1 ,降序：0"<<endl;
    cin>>k;
    if(k == 1){
        showList();
        
    }else{
        int n = this->num;
        cout<<"==========================="<<endl;
        cout<<"当前人数： "<<n<<endl;
        for(int i = n-1; i > -1;i--){
            this->empArry[i]->showInfo();
        }
        cout<<"==========================="<<endl;
    }
}

